package com.yumeko.springcloud.controller;

import com.yumeko.springcloud.entity.TPayment;
import com.yumeko.springcloud.service.TPaymentService;
import com.yumeko.springcloud.util.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * (TPayment)表控制层
 *
 * @author Zheng Hao
 * @since 2020-03-07 03:02:21
 */
@RestController
@RequestMapping("tPayment")
@Slf4j
public class TPaymentController {
    /**
     * 服务对象
     */
    @Resource
    private TPaymentService tPaymentService;

    @Resource
    private DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private String serverport;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ResponseData selectOnePayment(Long id) {
        return this.tPaymentService.queryById(id);
    }

    //http://localhost:8001/tPayment/selectOne1/1
    @GetMapping("/selectOne1/{id}")
    public ResponseData selectOnePayment1(@PathVariable("id") Long id) {
        return this.tPaymentService.queryById(id);
    }

    @PostMapping("insert")
    @ResponseBody
    public ResponseData insertPayment(@RequestBody TPayment tPayment) {
        return this.tPaymentService.insert(tPayment);
    }

    @GetMapping("/payment/discovery")
    public Object getDiscovery() {
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("======service======" + service);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PROVIDER-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info("#######instance########" + "\t" + instance.isSecure() + "\t" + instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
        }
        return this.discoveryClient;
    }
    //http://localhost:8001/tPayment//payment/lb
    @GetMapping("/payment/lb")
    public String getPaymentLB() {
        return serverport;
    }

    @GetMapping("/payment/feign/timeout")
    public String paymentFeignTimeOut() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverport;
    }
    //http://localhost:8001/tPayment/payment/zipkin
    @GetMapping("/payment/zipkin")
    public String paymentZipkin() {
        return "start zipkin 哈哈o(∩_∩)o 哈哈";
    }
}